import base64
import random
import hashlib
from typing import Union

def generate_random_ipv4_address():
    first_octet = random.randint(1, 254)
    second_octet = random.randint(0, 255)
    third_octet = random.randint(0, 255)
    fourth_octet = random.randint(0, 255)
    return f"{first_octet}.{second_octet}.{third_octet}.{fourth_octet}"


def calculate_md5(data: str, return_hex: bool = False) -> Union[bytes, str]:
    md5 = hashlib.md5()
    md5.update(data.encode())
    return md5.hexdigest() if return_hex else md5.digest()


def binary_to_base64(binary_data: bytes) -> str:
    """
    将二进制数据转换为 Base64 字符串

    Args:
        binary_data: 二进制数据

    Returns:
        Base64 字符串
    """

    # 使用 base64 模块将二进制数据转换为 Base64 编码
    base64_encoded_data = base64.b64encode(binary_data)

    # 将 Base64 编码的数据解码为字符串
    base64_string = base64_encoded_data.decode('utf-8')

    return base64_string
